package com.gxz.message.mq.listener;

import com.alibaba.fastjson.JSON;
import com.gxz.message.websocket.WebSocketServer;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/*****
 * @Author: http://www.itheima.com
 * @Description: com.itheima.message.mq.listener.MoneyQueueListener
 ****/
@Component
public class MoneyQueueListener {

    @Autowired
    private WebSocketServer webSocketServer;

    /***
     * 监听队列money
     */
    @RabbitListener(queues = "redmessage.queue")
    public void moneyList(String message) throws IOException {
        try {
            Map<String,Object> map = JSON.parseObject(message,Map.class);
            System.out.println("消费信息："+message);
            Integer code = Integer.parseInt(map.get("code").toString());
            if(code!=null && code==200){
                webSocketServer.sendMessage(WebSocketServer.message("恭喜你，成功抢到红包！",map.get("user").toString(),200,map.get("money").toString(),201),map.get("user").toString());
            }else if(code==204){
                //抢红包失败
                webSocketServer.sendMessage(WebSocketServer.message("来晚了，红包一抢而空！",map.get("user").toString(),204,map.get("money").toString(),201),map.get("user").toString());
            }
        } catch (Exception e) {
        }
    }
}
